package com.atguigu4.dao;

import com.beans.Order;
import com.utils.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

import static org.junit.Assert.*;

/**
 * @auther user
 * @date 2021/12/10 - 6:42
 */
public class OrderDaoImplTest {
    OrderDaoImpl dao = new OrderDaoImpl();
    @Test
    public void inert() {
        Connection connection = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date parse = simpleDateFormat.parse("2021-09-09");
            Order order = new Order(0,"name",new Date(parse.getTime()));
            //获取连接
            connection = JDBCUtils.getConnection3();
            //事务 不允许自动提交
            connection.setAutoCommit(false);
            int i = dao.inert(connection, order);
            if (i == 1){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚操作
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    public void deleteById() {
        Connection connection = null;
        try {
            //获取连接
            connection = JDBCUtils.getConnection3();
            //事务 不允许自动提交
            connection.setAutoCommit(false);
            int i = dao.deleteById(connection,5);
            if (i == 1){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚操作
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    public void updateById() {
        Connection connection = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date parse = simpleDateFormat.parse("2021-09-09");
            Order order = new Order(6,"更新后",new Date(parse.getTime()));
            //获取连接
            connection = JDBCUtils.getConnection3();
            //事务 不允许自动提交
            connection.setAutoCommit(false);
            int i = dao.updateById(connection,order);
            if (i == 1){
                System.out.println("更新成功");
            }else {
                System.out.println("更新失败");
            }
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚操作
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    public void getCustomersById() {
        Connection connection = null;
        try {
            //获取连接
            connection = JDBCUtils.getConnection3();
            //事务 不允许自动提交
            connection.setAutoCommit(false);
            Order order = dao.getOrderById(connection, 6);
            System.out.println("order = " + order);
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚操作
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    public void getAll() {
        Connection connection = null;
        try {
            //获取连接
            connection = JDBCUtils.getConnection3();
            //事务 不允许自动提交
            connection.setAutoCommit(false);
            List<Order> orderList = dao.getAll(connection);
            orderList.forEach(System.out::println);
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚操作
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    public void getCount() {
        Connection connection = null;
        try {
            //获取连接
            connection = JDBCUtils.getConnection3();
            //事务 不允许自动提交
            connection.setAutoCommit(false);
            Long count = dao.getCount(connection);
            System.out.println("count = " + count);
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚操作
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource(connection,null);
        }
    }
}